﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace PTExpress.SysLib
{
    public class SysOptions
    {
        static string StartupPath = System.Windows.Forms.Application.StartupPath;
        static string OptionsPath = StartupPath + "\\Data\\options.xml";
        static DataSet ds = new DataSet();
        //Load Optoin
        static public void Load()
        {
            ds = new DataSet();
            if (!System.IO.File.Exists(OptionsPath))
            {
                DataTable tb = new DataTable("options");
                tb.Columns.Add("id");
                tb.Columns.Add("content");
                tb.Rows.Add("Language", "V");
                tb.Rows.Add("ConnectionString", "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Data\\PTExpress.mdf;Integrated Security=True");
                ds.Tables.Add(tb.Copy());
            }
            else
                ds.ReadXml(OptionsPath);
        }

        //Save Option
        static public void Save()
        {
            ds.WriteXml(OptionsPath);
        }

        //Lấy RememberLogin
        static public string GetOption(Options id)
        {
            string RLogin = string.Empty;
            DataRow[] row = ds.Tables[0].Select("id = '" + id.ToString() + "'");
            if (row.Length <= 0)
                return RLogin;
            RLogin = row[0]["content"].ToString();
            return RLogin;
        }

        //Lấy RememberLogin
        static public string GetOption(string id)
        {
            string RLogin = string.Empty;
            DataRow[] row = ds.Tables[0].Select("id = '" + id + "'");
            if (row.Length <= 0)
                return RLogin;
            RLogin = row[0]["content"].ToString();
            return RLogin;
        }

        //Update option
        static public void UpdateOption(Options key, string content)
        {
            if (ds.Tables[0].Select("id = '" + key + "'").Length > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    if (ds.Tables[0].Rows[i]["id"].ToString().Contains(key.ToString()))
                    {
                        ds.Tables[0].Rows[i]["content"] = content;
                        break;
                    }
            }
            else
                ds.Tables[0].Rows.Add(key, content);
        }

        //Update option
        static public void UpdateOption(string key, string content)
        {
            if (ds.Tables[0].Select("id = '" + key + "'").Length > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    if (ds.Tables[0].Rows[i]["id"].ToString().Contains(key))
                    {
                        ds.Tables[0].Rows[i]["content"] = content;
                        break;
                    }
            }
            else
                ds.Tables[0].Rows.Add(key, content);
        }

    }

    //Enum Option
    public enum Options
    {
        RemmemberLogin,
        ConnectionString,
        Language
    }

}
